﻿using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace Magic.Web.Entry;
/// <summary>
/// 菜单服务
/// </summary>
public class SysMenuController : BaseController
{
    private readonly IMenuService _service;
    public SysMenuController(IMenuService service)
    {
        _service = service;
    }

    [HttpPost("getIndex")]
    public async Task<dynamic> GetIndex([FromBody]MenuInput input)
    {
        if (!input.IsTree)
        {
            var list = await _service.GetTree(all: true,queck_search:input.quick_search);
            return new
            {
                list,
                remark = ""
            };
        }
        else
        {
            return await _service.GetPageList(input);
        }
        
    }
    /// <summary>
    /// 根据id返回Menu
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("getDetail")]
    public async Task<dynamic> GetDetail([FromQuery][Required(ErrorMessage ="Id不能为空")]int id)
    {
        return await _service.Get(id);
    }
    /// <summary>
    /// 新增菜单规则
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("add")]
    public async Task Add(AddMenuRuleInput input)
    {
        await _service.Add(input);
    }
    /// <summary>
    /// 更新菜单规则
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("edit")]
    public async Task Edit(EditMenuRuleInput input)
    {
        await _service.Edit(input);
    }
    /// <summary>
    /// 删除菜单规则
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task Delete([FromQuery] string ids)
    {
        await _service.Delete(ids.SplitToIntList().ToList());
    }
    /// <summary>
    /// 更新菜单排序
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("sortable")]
    public async Task Sortable(SortMenuRuleInput input)
    {
        await _service.Sort(input);
    }
}
